//
//  ViewController.swift
//  Swift各种传值
//
//  Created by Bao on 15/12/10.
//  Copyright © 2015年 Microlink. All rights reserved.
//

import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,chagngeTitleDelegate {

     let titlArray:NSArray? = NSArray(objects:"代理","单例","通知","闭包")
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let tableview:UITableView? = UITableView(frame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height), style: .Plain)
        tableview!.delegate = self
        tableview!.dataSource = self
        
        self.view!.addSubview(tableview!)
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "gameOver:", name: "noti", object: nil)
      
        
        // Do any additional setup after loading the view, typically from a nib.
    }
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 50
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return (titlArray?.count)!
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        var cell:UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("cell")
        if(cell == nil)
        {
            cell = UITableViewCell(style: .Default, reuseIdentifier: "cell")
        }
        cell?.textLabel?.text = titlArray![indexPath.row] as? String
        return cell!
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let rows:Int! = indexPath.row 
        switch(rows)
        {
        case 0:
            let dele:DelegateViewController = DelegateViewController()
            self.navigationController!.pushViewController(dele, animated: true)
            dele.delegate = self

        case 1:
            let dele:SingletonViewController = SingletonViewController()
            self.navigationController!.pushViewController(dele, animated: true)

        case 2:
            let dele:NotifViewController = NotifViewController()
            self.navigationController!.pushViewController(dele, animated: true)
        default :
        
            let dele:BlockViewController = BlockViewController()
            self.navigationController!.pushViewController(dele, animated: true)
            //block
            dele.initChange({ (string) -> (Void) in
                self.navigationItem.title = string
            })
                
            
            
        }
        
    }
    
    //代理
    func changeNavTitle(string: String) {
      //  print(string);
        self.navigationItem.title = string
    }
    
    
    //单例
//    override func viewWillAppear(animated: Bool) {
//        let sign:SingletonClass? = SingletonClass.shareInstance()
//        
//        self.navigationItem.title = sign?.str
//    }
    //通知
    func gameOver(title:NSNotification)
    {
        let str = title.object as! String
       
        self.navigationItem.title = str
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

